﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Net;
using System.IO;
using System.ServiceModel;

namespace SuperKnowledgeDatabase2000Client
{
    /// <summary>
    /// Interaktionslogik für Window1.xaml
    /// </summary>
    public partial class RegisterReferenceAddress : Window
    {

        public RegisterReferenceAddress()
        {
            InitializeComponent();
        }

        protected override void OnContentRendered(EventArgs e)
        {
            this.Activate();
            base.OnContentRendered(e);
            txt_rootAddress.Focus();
            if (Properties.Settings.Default.EndPointRootAddress.Length > 0)
            {
                txt_rootAddress.Text = Properties.Settings.Default.EndPointRootAddress;
                txt_rootAddress.SelectAll();
            }
            
            
        }


        private void BtnOkClick(object sender, RoutedEventArgs e)
        {
            string endpointAddress = "";
            if (txt_rootAddress.Text.Length < 8 || txt_rootAddress.Text.Substring(0, 7) != "http://")
            {
                endpointAddress = "http://" + txt_rootAddress.Text;
            }
            else
            {
                endpointAddress = txt_rootAddress.Text;
            }
            if (txt_rootAddress.Text[txt_rootAddress.Text.Length - 1].Equals("/"))
            {
                endpointAddress = endpointAddress.Substring(0, endpointAddress.Length - 1);
            }
            string response = GetUrlResponse(endpointAddress);
            if (response == "Webservice not found")
            {
                MessageBox.Show("Webservice op '" + endpointAddress + "' niet bereikbaar", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                //  MessageBox.Show("Webservice op '" + endpointAddress + "' gevonden","OK" , MessageBoxButton.OK, MessageBoxImage.Information);
                Properties.Settings.Default.EndPointRootAddress = endpointAddress;
                Properties.Settings.Default.Save();
                ConfigSettings.SaveEndpointAddress(Properties.Settings.Default.EndPointRootAddress);
                try
                {
                    if (this.Owner.Name == "MainWindow")
                    {
                        // do nothing
                    }
                }
                catch (Exception ex)
                {
                    RegisterUser regUser = new RegisterUser();
                    regUser.Show();
                }
                this.Close();
            }
        }


        public static string GetUrlResponse(string url)
        {
            string content = null;
            try
            {
                WebRequest webRequest = WebRequest.Create(url + "/KMWebService/Kennismanagement?wsdl");
                WebResponse webResponse = webRequest.GetResponse();
                StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.ASCII);
                StringBuilder contentBuilder = new StringBuilder();
                while (-1 != sr.Peek())
                {
                    contentBuilder.Append(sr.ReadLine());
                    contentBuilder.Append("\r\n");
                }
                content = contentBuilder.ToString();
            }
            catch (Exception ex)
            {
             
                    content = "Webservice not found";

            }

            return content.ToString();
        }


        private void OnKeyDownHandler(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                BtnOkClick(sender, e);
            }
        }
    }
}
